//日期月份相加减
function addDate(date, months) {
	var d = new Date(date);
	var m = d.getMonth() + months;
	return d.getFullYear() + '-' + m + '-' + d.getDate();
}

//初始化时间
function initDate() {
	/*$("#startTime").val(CurrentDate());
	$("#endTime").val(CurrentDate());
	
	$("#moneyStartTime").val(CurrentDate());
	$("#moneyEndTime").val(CurrentDate());*/
	
	$("#startTime").val(addDate(new Date(), 0));
	$("#endTime").val(addDate(new Date(), 1));
	
	$("#moneyStartTime").val(addDate(new Date(), 0));
	$("#moneyEndTime").val(addDate(new Date(), 1));
};

//初始化界面数据
function initTradeDetail() {
	$.ajax({
		type : "post",
		url : "/finance/tradeByCondition",
		data : {
			initTradeDetail : "true"
		},
		dataType : "json",
		success : function(data) {
			var tb_content = '';
			if (data.dList.length > 0) {
				var inOUtMoney = null;
				$.each(data.dList, function(index, deal) {
					if (deal.dealState == "2" || deal.dealState == "3") {
						inOUtMoney = deal.disburseMoney;
					} else {
						inOUtMoney = deal.incomeMoney;
					}
					tb_content += '<tr id="' + deal.dealId
							+ '"><td style="border: 0px">' + '<font>'
							+ deal.dealNum + '</font></td>'
							+ '<td style="border: 0px"><font>'
							+ toDate(deal.dealDate, "yyyy-MM-dd hh:mm:ss")
							+ '</font></td><td style="border: 0px"><font>'
							+ deal.dealName
							+ '</font></td><td style="border: 0px"><font>'
							+ isNull(deal.belongCompanyName)
							+ '</font></td><td style="border: 0px"><font>'
							+ checkdealState(deal.dealState)
							+ '</font></td><td style="border: 0px">' + '<font>'
							+ parseFloat(inOUtMoney).toFixed(2)
							+ '</font></td>' + '<td style="border: 0px"><font>'
							+ payWay(deal.payWayId) + '</font></td></tr>'
				})
			}
			$("#tradeDetailList0").append(tb_content);
		}
	});
}

// 是否为空判断
function isNull(data) {
	if (data == null) {
		return "";
	} else {
		return data;
	}
}
// 交易状态显示
function checkdealState(dealState) {
	if (dealState != null && dealState != '') {
		if (dealState == "1") {
			return "充值";
		} else if (dealState == "2") {
			return "消费";
		} else if (dealState == "3") {
			return "提现";
		} else if (dealState == "4") {
			return "退款";
		} else {
			return "失败";
		}
	} else {
		return "";
	}
}

//现金收支明细交易状态显示
function checkdealType(dealType) {
	if (dealType == "1") {
		return "充值";
	} else if (dealType == "2") {
		return "消费";
	} else if (dealType == "3") {
		return "提现";
	} else if (dealType == "4") {
		return "退款";
	} else if (dealType == "5") {
		return "其他";
	}
}

//时间区间验证
function checkTime(startTime,endTime){
	var startTime = startTime.replace(/-/g,"/");
	var endTime = endTime.replace(/-/g,"/");
	if(startTime == '' || endTime == ''){
		BSTYUN.dialog.warn('请填写正确的时间区间！');
		return false;
	}else if(new Date(startTime).getTime() <= new Date(endTime).getTime()){
		return true;
	}else{
		BSTYUN.dialog.warn('请填写正确的时间区间！');
		return false;
	}
}

$(function() {
	// 初始化日期
	initDate();
	// 初始化查询交易明细
	initTradeDetail();
	//绑定返回收支明细事件
	var $cash_list = $(".cash_list"),
		$cash_detail = $(".cash_detail"),
		$return = $("#returnCashList");
	$return.on("click",function(){
		$cash_list.show();
		$cash_detail.hide();
		$return.hide();
	});
	//绑定tab点击事件
	$("#myTab").on("click","li",function(){
		$return.trigger("click");
	});
	/*时间区间事件*/
	timeRangeSearch();
})

// 交易明细查询
function dealDetail() {
	var startTime = $('#startTime').val();
	var endTime = $('#endTime').val();
	var dealType = $('#dealType').val();
	if(!checkTime(startTime,endTime)) return; 
	$.ajax({
		type : "POST",
		url : "/finance/tradeByCondition",
		data : {
			startTime : startTime,
			endTime : endTime,
			dealState : dealType
		},
		success : function(data, response) {
			$("#tradeDetailList0").empty();
			var tb_content = '';
			if (data.dList.length > 0) {
				$.each(data.dList, function(index, deal) {
					var inOUtMoney = null;
					if (deal.dealState == "2" || deal.dealState == "3") {
						inOUtMoney = deal.disburseMoney;
					} else {
						inOUtMoney = deal.incomeMoney;
					}
					tb_content += '<tr id="' + deal.dealId
							+ '"><td style="border: 0px">' + '<font>'
							+ deal.dealNum + '</font></td>'
							+ '<td style="border: 0px"><font>'
							+ toDate(deal.dealDate, "yyyy-MM-dd hh:mm:ss")
							+ '</font></td><td style="border: 0px"><font>'
							+ deal.dealName
							+ '</font></td><td style="border: 0px"><font>'
							+ deal.belongCompanyName
							+ '</font></td><td style="border: 0px"><font>'
							+ checkdealState(deal.dealState)
							+ '</font></td><td style="border: 0px">' + '<font>'
							+ parseFloat(inOUtMoney).toFixed(2)
							+ '</font></td>' + '<td style="border: 0px"><font>'
							+ payWay(deal.payWayId) + '</font></td></tr>';
				})
			}
			$("#tradeDetailList0").append(tb_content);
		}
	})
}

// 现金收支明细查询
function moneyDetailQuery() {
	// 现金收支明细查询
	var startTime = $("#moneyStartTime").val();
	var endTime = $("#moneyEndTime").val();
	var dealType = $("#dealType").val();
	if(!checkTime(startTime,endTime)) return; 
	$.ajax({
		type : "POST",
		url : "/finance/balanceByCondition",
		data : {
			startTime : startTime,
			endTime : endTime,
			dealType : dealType 
		},
		success : function(data, response) {
			$("#tradeDetailList1").empty();
			var tb_content = '';
			if (data.dList.length > 0) {
				$.each(data.dList,function(index, deal) {
					var userName = deal.userEmail; 
					if(userName == ""){
						userName = deal.userTell;
					}
					tb_content += '<tr id="'
						+ deal.cashflowId
						+ '">'
						+ '<td style="border: 0px">'
						+ '<font>'
						+ deal.cashflowNum
						+ '</font></td>'
						+ '<td style="border: 0px">'
						+ '<font>'
						+ checkdealType(deal.dealType)
						+ '</font></td>'
						+ '<td style="border: 0px"><font>'
						+ toDate(deal.createTime,"yyyy-MM-dd hh:mm:ss")
						+ '</font></td>'
						+ '<td style="border: 0px"><font>'
						+ formatInMoney(deal)
						+ '</font></td>'
						+ '<td style="border: 0px"><font>'
						+ formatDisburseMoney(deal)
						+ '</font></td><td style="border: 0px"><font>'+deal.thenAccount+'</font></td>'
						+ '<td style="border: 0px"><font>'+payWay(deal.payWayId)+'</font></td>'
						+ '<td style="border: 0px"><font><a href="javascript:lookDetail('+deal.cashflowId+','+deal.dealType+')">查看详情</a></font></td></tr>';
				})
			}else{
				tb_content += '<tr><td colspan="9">查无记录</td></tr>';
			}
			$("#tradeDetailList1").append(tb_content);
		}
	})
}

//时间区间事件
function timeRangeSearch(){
	var $timerange = $("#timeRange"),
		$end = $("#moneyEndTime"),
		$start = $("#moneyStartTime"),
		$search = $("#monerSearch");
	$timerange.on("change",function(){
		var $this = $(this),
			txt = parseInt($this.val()),
			startTime = new Date();
		if(txt == 1) startTime.setDate(startTime.getDate() - 7);
		if(txt == 2) startTime.setDate(startTime.getDate() - 15);
		if(txt == 3)startTime.setMonth(startTime.getMonth() - 1);
		if(txt == 4)startTime.setFullYear(startTime.getFullYear() - 1);

		$end.val(formatDate(new Date(),"yyyy-MM-dd"));
		$start.val(formatDate(startTime,"yyyy-MM-dd"));
		$search.trigger("click");
	})
}

//现金收支明细查看详情
function lookDetail(cashflowId,dealType){
	var $cash_list = $(".cash_list"),
		$cash_detail = $(".cash_detail"),
		$return = $("#returnCashList");
	$.ajax({
		type : "POST",
		url : "/finance/balanceDetail/"+cashflowId+"/"+dealType,
		success : function(data) { 

			var cashflowVo = data.cashflowVo;
			
			//详细card展现
			$cash_list.hide();
			$cash_detail.show();
			$return.show();
			
			//放数据
			$('#cashflowNum').text(cashflowVo.cashflowNum);
			//时间格式处理
			var createTime = new Date(cashflowVo.createTime);
			createTime = toDate(createTime, "yyyy-MM-dd hh:mm:ss");
			$('#createTime').text(createTime);
			$('#actionType').text(checkdealType(cashflowVo.dealType));
			
			var t = "充值";
			if(cashflowVo.orderNum != null) {
				t = cashflowVo.orderNum;
			}
			$('#orderNum').text(t);
			$('#thenAccount').text(parseFloat(cashflowVo.thenAccount).toFixed(2));
			//放操作金额
			var actionMoney = cashflowVo.disburseMoney;
			if(actionMoney == 0) actionMoney = cashflowVo.incomeMoney;
			$('#actionMoney').text(parseFloat(actionMoney).toFixed(2));
			$('#blockMoney').text(parseFloat(actionMoney).toFixed(2));
			
			$('#companyDomainName').text(isNull(cashflowVo.companyDomainName));
			$('#belongCompanyName').text(isNull(cashflowVo.belongCompanyName));
			
			$('#remark').text(payWay(cashflowVo.payWayId));
		},
		error: function(){
			BSTYUN.dialog.warn("网络异常！请稍后再试")
		}
	});
	//location.href="/finance/balanceDetail/"+cashflowId+"/"+dealType;
}

//收入显示
function formatInMoney(deal) {
	
	if(deal.dealType == 1){
		if(deal.incomeMoney == "0" ){
			return "+0.00";
		}else{
			var incomeMoney = '+' + parseFloat(deal.incomeMoney).toFixed(2)
			return incomeMoney;
		}
	}else{
		return "";
	}
}

// 支出显示
function formatDisburseMoney(deal) {
	if(deal.dealType == 2){
		if(deal.disburseMoney == "0" ){
			return "-0.00";
		}else{
			var disburseMoney = '-' + parseFloat(deal.disburseMoney).toFixed(2)
			return disburseMoney;
		}
	}else{
		return "";
	}
	
}
// 获取支付方式名称：
function payWay(payWayId) {
	if (payWayId != null && payWayId != "") {
		if (payWayId == "1") {
			return "支付宝支付";
		} else if (payWayId == "2") {
			return "账户余额支付";
		} else if (payWayId == "3") {
			return "银联支付";
		} else if (payWayId == "4") {
			return "线下支付";
		}
	} else {
		return "";
	}
}
// tab切换
function nTabs(thisObj, Num) {
	if ('0' == Num) {
		$("#myTab_Content0").css("display", "block");
		$("#myTab_Content1").css("display", "none");
		var startTime = $("#startTime").val();
		var endTime = $("#endTime").val();
		if (startTime != "" && startTime != null) {
			$("#startTime").val(startTime)
			$("#endTime").val(endTime);
		} else {
			startTime = $("#startTime").val();
			endTime = $("#endTime").val();
		}
		$(thisObj).attr("class", "active");
		$(thisObj).next().attr("class", "sta");
		if(!checkTime(startTime,endTime)) return; 
		$.ajax({
			type : "POST",
			url : "/finance/tradeByCondition",
			data : {
				startTime : startTime,
				endTime : endTime 
			},
			success : function(data, response) {
				$("#tradeDetailList0").empty();
				var tb_content = '';
				if (data.dList.length > 0) {
					$.each(data.dList, function(index, deal) {
						if (deal.dealState == "2" || deal.dealState == "3") {
							inOUtMoney = deal.disburseMoney;
						} else {
							inOUtMoney = deal.incomeMoney;
						}
						tb_content += '<tr id="' + deal.dealId
								+ '"><td style="border: 0px">' + '<font>'
								+ deal.dealNum + '</font></td>'
								+ '<td style="border: 0px"><font>'
								+ toDate(deal.dealDate, "yyyy-MM-dd hh:mm:ss")
								+ '</font></td><td style="border: 0px"><font>'
								+ deal.dealName
								+ '</font></td><td style="border: 0px"><font>'
								+ deal.belongCompanyName
								+ '</font></td><td style="border: 0px"><font>'
								+ checkdealState(deal.dealState)
								+ '</font></td><td style="border: 0px">'
								+ '<font>' + parseFloat(inOUtMoney).toFixed(2)
								+ '</font></td>'
								+ '<td style="border: 0px"><font>'
								+ payWay(deal.payWayId) + '</font></td></tr>';
					})
				}
				$("#tradeDetailList0").append(tb_content);
			}
		})
		$("#myTab_Content1").css("display", "none");
		$("#myTab_Content0").css("display", "block");

	} else {
		$("#myTab_Content0").css("display", "none");
		$("#myTab_Content1").css("display", "block");
		
		var startTime = $("#moneyStartTime").val();
		var endTime = $("#moneyEndTime").val();
		var dealType = $("#dealType").val();
		var dealTab = $("#tradeDetailList1");

		$(thisObj).attr("class", "active");
		$(thisObj).prev().attr("class", "sta");
		if (startTime != "" && startTime != null) {
			$("#moneyStartTime").val(startTime)
			$("#moneyEndTime").val(endTime);
		} else {
			startTime = $("#moneyStartTime").val();
			endTime = $("#moneyEndTime").val();
		}

		if(!checkTime(startTime,endTime)) return; 
		$.ajax({
			type : "POST",
			url : "/finance/balanceByCondition",
			data : {
				startTime : startTime,
				endTime : endTime,
				dealType : dealType 
			},
			success : function(data, response) {
				$("#tradeDetailList1").empty();
				var tb_content = '';
				if (data.dList.length > 0) {
					$.each(data.dList,function(index, deal) {
						var userName = deal.userEmail; 
						if(userName == ""){
							userName = deal.userTell;
						}
						tb_content += '<tr id="'
								+ deal.cashflowId
								+ '">'
								+ '<td style="border: 0px">'
								+ '<font>'
								+ deal.cashflowNum
								+ '</font></td>'
								+ '<td style="border: 0px">'
								+ '<font>'
								+ checkdealType(deal.dealType)
								+ '</font></td>'
								+ '<td style="border: 0px"><font>'
								+ toDate(deal.createTime,
										"yyyy-MM-dd hh:mm:ss")
								+ '</font></td>'
								+ '<td style="border: 0px"><font>'
								+ formatInMoney(deal)
								+ '</font></td>'
								+ '<td style="border: 0px"><font>'
								+ formatDisburseMoney(deal)
								+ '</font></td><td style="border: 0px"><font>'+deal.thenAccount+'</font></td>'
								+ '<td style="border: 0px"><font>'+payWay(deal.payWayId)+'</font></td>'
								+ '<td style="border: 0px"><font><a href="javascript:lookDetail('+deal.cashflowId+','+deal.dealType+')">查看详情</a></font></td></tr>';
					})
				}
				$("#tradeDetailList1").append(tb_content);
			}
		})
		$("#myTab_Content1").css("display", "block");
		$("#myTab_Content0").css("display", "none");
	}
}